import {Component, OnInit} from '@angular/core';
import {SettingService} from '../setting.service';
import {Setting} from '../../../public/setting/setting';

@Component({
  selector: 'app-app-home',
  templateUrl: './app-home.component.html',
  styleUrls: ['./app-home.component.css']
})
export class AppHomeComponent implements OnInit {
  public appHomeData: any;
  public isEdit: boolean = false;
  public isConfirmLoading: boolean = false;
  public nativeData: any;
  public msgTip = Setting.msgTip;
  public ckEditerConfig = {
    toolbarGroups: [
      {name: 'links'},
      {name: 'tools'},
      {name: 'basicstyles', groups: ['basicstyles']},
      {name: 'paragraph', groups: ['list', 'indent', 'blocks', 'align', 'bidi']},
    ]
  };

  constructor(private settingService: SettingService) {
  }

  ngOnInit() {
    this.loadByK();
  }

  cancelEdit() {
    this.isEdit = false;
    this.appHomeData = this.nativeData;
  }

  loadByK() {
    this.settingService.loadByK(Setting.appHomeDesKey).then((res: any) => {
      this.appHomeData = res.v;
      this.nativeData = res.v;
    });
  }

  modifyAppHomeDara($event) {
    $event.preventDefault();
    this.isConfirmLoading = true;
    this.settingService.modify({
      k: Setting.appHomeDesKey,
      v: this.appHomeData
    }).then(res => {
      this.isEdit = false;
      this.isConfirmLoading = false;
      this.loadByK();
    }).catch(err => {
      this.isConfirmLoading = false;
    });
  }

}
